package com.nageoffer.jobtrain.projects.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户提交代码限流配置类
 */
@Component
public class SentinelRuleConfig implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule submitCodeRule = new FlowRule();
        submitCodeRule.setResource("oj-question-submit-code");
        submitCodeRule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 限流类型：QPS
        submitCodeRule.setCount(1); // 每秒允许的请求数：1 QPS = 每 1 秒 1 次
        rules.add(submitCodeRule);
        FlowRuleManager.loadRules(rules);
    }
}